/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.jsf;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.brainstretch.ejb.managers.QuizManager;
import org.brainstretch.jsf.util.JsfUtil;
import org.brainstretch.models.quizzes.Quiz;

/**
 *
 * @author mrodaitis
 */
@ManagedBean
@ViewScoped
public class QuizController implements Serializable{

     private Quiz quiz;  
     
     @EJB
     private QuizManager quizManager;
    
    /**
     * Creates a new instance of QuizController
     */
    public QuizController() {
    }
    
    public Quiz getQuiz(){
    if(quiz == null){
        String quizIdParam = JsfUtil.getRequestParameter("quizId");
        long quizId = -1;
        if(quizIdParam != null){
            try{
                quizId = Long.parseLong(quizIdParam);
            }catch(NumberFormatException ex){
                quizId = -1;
            }
        }
        quiz = quizManager.findById(quizId);
        if(quiz == null){
            JsfUtil.addErrorMessage("Are you sure quiz exists?");
        }
    }
    return quiz;
    }
}
